package com.taotao.manager.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.bean.EasyUIResult;
import com.taotao.common.service.ApiHttpClientService;
import com.taotao.manager.mapper.ItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;

@Service
public class ItemService extends BaseService<TbItem> {

	@Autowired
	private ItemMapper itemMapper;
	
	@Autowired
	private ItemDescService itemDescService;
	
	@Autowired
	private ItemParamItemService itemParamItemService;
	
	@Autowired
	private ApiHttpClientService httpClientService;
	
	@Autowired
	private RabbitTemplate rabbitTemplate;
	
	private static final ObjectMapper MAPPER = new ObjectMapper();
	
	public Boolean saveItem(TbItem item, String desc, String itemParams) {
        // 初始数据
        item.setStatus(1);
        item.setId(null);// 强制设置id为null，从数据库中自增长

        Integer count1 = super.save(item);
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);

        // 保存商品描述数据
        Integer count2 = this.itemDescService.save(itemDesc);

        // 保存规格参数数据
        TbItemParamItem itemParamItem = new TbItemParamItem();
        itemParamItem.setItemId(item.getId());
        itemParamItem.setParamData(itemParams);
        this.itemParamItemService.save(itemParamItem);

        // 发送消息
        sendMsg(item.getId(), "insert");

        return count1.intValue() == 1 && count2.intValue() == 1;
    }

	/**
	 * 查询商品列表
	 * <p>Title: queryItemList</p>
	 * <p>Description: </p>
	 * @param page
	 * @param rows
	 * @return
	 */
	public EasyUIResult queryItemList(int page, int rows) {
		PageHelper.startPage(page,rows);
		
		Example example = new Example(TbItem.class);
		example.setOrderByClause("created DESC");
		List<TbItem> items = this.itemMapper.selectByExample(example);
		
		PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(items);
		
		return new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
	}

	public Boolean updateItem(TbItem item, String desc, Long itemParamId, String itemParams) {
        item.setCreated(null);
        item.setStatus(null);
		Integer count1 = super.updateSelective(item);
      
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(item.getId());
        itemDesc.setItemDesc(desc);
 
        // 保存商品描述数据
        Integer count2 = this.itemDescService.updateSelective(itemDesc);

        // 保存规格参数数据
        TbItemParamItem itemParamItem = new TbItemParamItem();
        itemParamItem.setId(itemParamId);
        itemParamItem.setParamData(itemParams);
        this.itemParamItemService.updateSelective(itemParamItem);

      /*  
        //通知其他系統更新緩存,不允许原来逻辑 TODO 待优化
        try {
			String url = ""+"item/cache/"+item.getId()+".html";
			httpClientService.doPost(url);
		} catch (Exception e) {
			e.printStackTrace();
		} */
        // 发送消息
        sendMsg(item.getId(), "update");

        return count1.intValue() == 1 && count2.intValue() == 1;
	}
	
	private void sendMsg(Long itemId, String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("itemId", itemId);
		map.put("type", type);
		map.put("date", System.currentTimeMillis());
		try {
			rabbitTemplate.convertAndSend("item.insert", MAPPER.writeValueAsString(map));
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
}
